import React, { Component } from "react";
import moment from "moment";

class Lifecycle extends Component {
  state = {
    time: moment().format("YYYY-MM-DD HH:mm:ss"),
  };
  timeinterval = null;

  componentDidMount() {
    console.log("组件挂载完成");
    this.timeinterval = setInterval(() => {
      this.setState({ time: moment().format("YYYY-MM-DD HH:mm:ss") });
    }, 1000);
  }

  componentDidUpdate() {
    console.log("组件更新·····");
  }

  componentWillUnmount() {
    console.log("卸载定时器");
    clearInterval(this.timeinterval);
  }

  render() {
    return (
      <>
        <div
          style={{
            height: "100px",
            width: "200px",
            border: "1px solid red",
            textAlign: "center",
            lineHeight: "100px",
          }}
        >
          {this.state.time}
        </div>
      </>
    );
  }
}

export default Lifecycle;
